/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlasm.sync;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import xmlasm.bo.FeedBO;
import xmlasm.bo.UserBO;
import xmlasm.bo.FeedbackBO;

/**
 * Web application lifecycle listener.
 * @author Hoang
 */
public class MyServletListener implements ServletContextListener {

    SynchronizeThread thread;

    public void contextInitialized(ServletContextEvent sce) {

        //Set duong dan mac dinh cua cac file xml
        String webPath = sce.getServletContext().getRealPath("/");
        String feedXMLPath = webPath + "WEB-INF/dataXML/feed.xml";
        String feedbackXMLPath = webPath + "WEB-INF/dataXML/feedback.xml";
        String userXMLPath = webPath + "WEB-INF/dataXML/user.xml";
        FilePathHolder.getInstance().setWebPath(webPath);
        FilePathHolder.getInstance().setFeedXMLPath(feedXMLPath);
        FilePathHolder.getInstance().setFeedbackXMLPath(feedbackXMLPath);
        FilePathHolder.getInstance().setUserXMLPath(userXMLPath);

        //Marshall san cai feed va user
        FeedBO.loadFeedsAndMarshall();
        UserBO.loadUsersAndMarshall();

        //Chay thread synchonize, that ra cung ko can synchonize
        thread = new SynchronizeThread();
        thread.setRunInBackground(true);
        thread.start();

    }

    public void contextDestroyed(ServletContextEvent sce) {
        thread.setRunInBackground(false);
    }
}
